package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zb.dto.DeviceDTO;
import com.zb.dto.DeviceInfoDTO;
import com.zb.dto.QuotaDTO;
import com.zb.dto.QuotaInfo;
import com.zb.entity.TbQuotaModel;
import com.zb.influx.InfluxRepository;
import com.zb.mapper.TbQuotaMapper;
import com.zb.service.TbQuotaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName TbQuotaServiceImpl
 * @Description 服务接口实现
 * @Author xm
 * @Date 2024/08/07 11:42
 **/
@Service
public class TbQuotaServiceImpl extends ServiceImpl<TbQuotaMapper, TbQuotaModel> implements TbQuotaService {

    @Autowired
    private InfluxRepository influxRepository;

    @Override
    public List<TbQuotaModel> queryNumberQuota() {
        LambdaQueryWrapper<TbQuotaModel> qw = new LambdaQueryWrapper<>();
        qw.eq(TbQuotaModel::getValueType, "Long")
                .or()
                .eq(TbQuotaModel::getValueType, "Double")
                .or()
                .eq(TbQuotaModel::getValueType, "Integer");
        return super.baseMapper.selectList(qw);
    }

    @Override
    public List<QuotaInfo> getLastQuotaList(String deviceId) {
        String ql = "select last(value),* from quota where deviceId='" + deviceId + "' group by quotaId";
        return influxRepository.query(ql, QuotaInfo.class);
    }

    @Override
    public void saveQuotaToInflux(List<QuotaDTO> quotaDTOList) {
        for (QuotaDTO quotaDTO : quotaDTOList) {
            QuotaInfo quotaInfo = new QuotaInfo();
            BeanUtils.copyProperties(quotaDTO, quotaInfo);
            quotaInfo.setQuotaId(quotaDTO.getId() + "");
            influxRepository.add(quotaInfo);
        }
    }

    /**
     * @param topic
     * @param payLoadMap {
     *                   "sn":"123456",
     *                   "temp":"12",
     *                   "humidity":50
     *                   }
     * @return
     */
    @Override
    public DeviceInfoDTO analysis(String topic, Map<String, Object> payLoadMap) {
        //根据主题查询指标列表
        LambdaQueryWrapper<TbQuotaModel> qw = new LambdaQueryWrapper<>();
        qw.eq(TbQuotaModel::getSubject, topic);
        List<TbQuotaModel> quotaList = this.list(qw);
        if (quotaList.size() == 0) return null;
        //封装设备信息 snKey=sn
        String snKey = quotaList.get(0).getSnKey();
        //根据sn获取设备编号
        String deviceId = payLoadMap.get(snKey).toString();
        DeviceDTO deviceDTO = new DeviceDTO();
        deviceDTO.setDeviceId(deviceId);
        //封装指标列表
        List<QuotaDTO> quotaDTOList = Lists.newArrayList();
        for (TbQuotaModel quota : quotaList) {
            String quotaKey = quota.getValueKey();//==temp humidity
            if (payLoadMap.containsKey(quotaKey)) {//map中包含对应的指标值
                QuotaDTO quotaDTO = new QuotaDTO();
                //将相同的属性值， 进行拷贝
                BeanUtils.copyProperties(quota, quotaDTO);
                quotaDTO.setQuotaName(quota.getName());
                //指标值封装
                //指标分为两种  1.数值  2.非数值（string boolean）
                //1.数值   value 存储数值  stringValue :存储数值字符串
                //2.非数值  value 0   stringValue:内容
                if ("String".equals(quotaDTO.getValueType())
                        || "Boolean".equals(quotaDTO.getValueType())) {
                    quotaDTO.setStringValue(payLoadMap.get(quotaKey).toString());
                    quotaDTO.setValue(0D);
                } else {
                    quotaDTO.setValue(Double.valueOf(payLoadMap.get(quotaKey).toString()));
                    quotaDTO.setStringValue(payLoadMap.get(quotaKey).toString());
                }
                quotaDTO.setDeviceId(deviceId);
                quotaDTOList.add(quotaDTO);
            }
        }
        DeviceInfoDTO deviceInfoDTO = new DeviceInfoDTO();
        deviceInfoDTO.setDeviceDTO(deviceDTO);
        deviceInfoDTO.setQuotaList(quotaDTOList);
        return deviceInfoDTO;
    }
}
